2
https://raw.githubusercontent.com/heridho118140173/TUBES-PBO-MASTER/master/TUBES-PBO-master/Bengkel1/src/tb_biaya.java
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import java.awt.HeadlessException;
import java.sql.Connection;
import javax.swing.table.DefaultTableModel;
import java.sql.SQLException;
import javax.swing.JOptionPane;
/**
 *
 * @author Heridho
 */
public class tb_biaya extends javax.swing.JFrame {
    
    private void kosongkan_foam(){
    txtKodeKonsumen.setEditable(true);
    cbKodeMontir.setSelectedItem(this);  
    txtKodeKonsumen.setText(null);
    txtKodeMobil.setText(null);
    txtKodeTransaksi.setText(null);
    txtTanggalTransaksi.setText(null);
    txtTotalTransaksi.setText(null);
}
    
private void tampilkan_data(){
        
        DefaultTableModel model = new DefaultTableModel();
        model.addColumn("No.");
        model.addColumn("Kode Montir");
        model.addColumn("Kode Konsumen");
        model.addColumn("Kode Mobil");
        model.addColumn("Kode Transaksi");
        model.addColumn("Tanggal Transaksi");
        model.addColumn("Total Transaksi");
        
        try{
            int no = 1;
            String sql = "SELECT * FROM tb_biaya";
            java.sql.Connection conn = (Connection)koneksi.getkoneksi();
            java.sql.Statement stm = conn.createStatement();
            java.sql.ResultSet res = stm.executeQuery(sql);
            
            while(res.next()){
                model.addRow(new Object[]{no++,res.getString(1),res.getString(2),res.getString(3),res.getString(4),res.getString(5),res.getString(6)});
            }
            tabelBiaya.setModel(model);
        }catch (SQLException e){
            System.out.println("Error : " + e.getMessage());
        }
        
    }


    /**
     * Creates new form tb_biaya
     */
    public tb_biaya() {
        initComponents();
        kosongkan_foam();
        tampilkan_data();
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabelBiaya = new javax.swing.JTable();
        txtKodeKonsumen = new javax.swing.JTextField();
        txtKodeMobil = new javax.swing.JTextField();
        txtKodeTransaksi = new javax.swing.JTextField();
        txtTanggalTransaksi = new javax.swing.JTextField();
        txtTotalTransaksi = new javax.swing.JTextField();
        btSimpan = new javax.swing.JButton();
        btHapus = new javax.swing.JButton();
        btEdit = new javax.swing.JButton();
        btKeluar = new javax.swing.JButton();
        cbKodeMontir = new javax.swing.JComboBox<String>();
        btTambah = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N
        jLabel1.setText("BIAYA");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(268, 28, -1, -1));

        jLabel3.setText("Kode Montir");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 96, -1, -1));

        jLabel4.setText("Kode Konsumen");
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 131, -1, -1));

        jLabel5.setText("Kode Mobil");
        getContentPane().add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 169, -1, -1));

        jLabel6.setText("Kode Transaksi");
        getContentPane().add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 207, -1, -1));

        jLabel7.setText("Tanggal Transaksi");
        getContentPane().add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 245, -1, -1));

        jLabel8.setText("Total Transaksi");
        getContentPane().add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 283, -1, -1));

        tabelBiaya.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tabelBiaya.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabelBiayaMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tabelBiaya);

        getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(115, 374, 436, 104));

        txtKodeKonsumen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtKodeKonsumenActionPerformed(evt);
            }
        });
        getContentPane().add(txtKodeKonsumen, new org.netbeans.lib.awtextra.AbsoluteConstraints(172, 128, 319, -1));
        getContentPane().add(txtKodeMobil, new org.netbeans.lib.awtextra.AbsoluteConstraints(172, 166, 319, -1));
        getContentPane().add(txtKodeTransaksi, new org.netbeans.lib.awtextra.AbsoluteConstraints(172, 204, 319, -1));
        getContentPane().add(txtTanggalTransaksi, new org.netbeans.lib.awtextra.AbsoluteConstraints(172, 242, 319, -1));
        getContentPane().add(txtTotalTransaksi, new org.netbeans.lib.awtextra.AbsoluteConstraints(172, 280, 319, -1));

        btSimpan.setText("Simpan");
        btSimpan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSimpanActionPerformed(evt);
            }
        });
        getContentPane().add(btSimpan, new org.netbeans.lib.awtextra.AbsoluteConstraints(242, 333, -1, -1));

        btHapus.setText("Hapus");
        btHapus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btHapusActionPerformed(evt);
            }
        });
        getContentPane().add(btHapus, new org.netbeans.lib.awtextra.AbsoluteConstraints(396, 333, -1, -1));

        btEdit.setText("Edit");
        btEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btEditActionPerformed(evt);
            }
        });
        getContentPane().add(btEdit, new org.netbeans.lib.awtextra.AbsoluteConstraints(327, 333, -1, -1));

        btKeluar.setText("Keluar");
        btKeluar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btKeluarActionPerformed(evt);
            }
        });
        getContentPane().add(btKeluar, new org.netbeans.lib.awtextra.AbsoluteConstraints(477, 333, -1, -1));

        cbKodeMontir.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "M007", "M067", "M069", "M076" }));
        getContentPane().add(cbKodeMontir, new org.netbeans.lib.awtextra.AbsoluteConstraints(172, 90, -1, -1));

        btTambah.setText("Tambah");
        btTambah.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btTambahActionPerformed(evt);
            }
        });
        getContentPane().add(btTambah, new org.netbeans.lib.awtextra.AbsoluteConstraints(153, 333, -1, -1));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btKeluarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btKeluarActionPerformed
        // TODO add your handling code here:
        System.exit(0);
    }//GEN-LAST:event_btKeluarActionPerformed

    private void btTambahActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btTambahActionPerformed
        // TODO add your handling code here:
        kosongkan_foam();
    }//GEN-LAST:event_btTambahActionPerformed

    private void btSimpanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSimpanActionPerformed
        // TODO add your handling code here:
        try{
            String sql = "INSERT INTO tb_biaya VALUES ('"+cbKodeMontir.getSelectedItem()+"','"+txtKodeKonsumen.getText()+"','"+txtKodeMobil.getText()+"','"+txtKodeTransaksi.getText()+"','"+txtTanggalTransaksi.getText()+"','"+txtTotalTransaksi.getText()+"')";
            java.sql.Connection conn = (Connection)koneksi.getkoneksi();
            java.sql.PreparedStatement pstm = conn.prepareStatement(sql);
            pstm.execute();
            JOptionPane.showMessageDialog(null,"Simpan data berhasil..");
            tampilkan_data();
            kosongkan_foam();
        }catch (HeadlessException | SQLException e){
            JOptionPane.showMessageDialog(this,e.getMessage());
        }
    }//GEN-LAST:event_btSimpanActionPerformed

    private void btEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btEditActionPerformed
        // TODO add your handling code here:
        try{
            String sql = "UPDATE tb_biaya SET kode_montir='"+cbKodeMontir.getSelectedItem()+"',kode_konsumen='"+txtKodeKonsumen.getText()+"',kode_mobil='"+txtKodeMobil.getText()+"',kode_transaksi='"+txtKodeTransaksi.getText()+"',tgl_transaksi='"+txtTanggalTransaksi.getText()+"',total_transaksi='"+txtTotalTransaksi.getText()+"' WHERE kode_konsumen = '"+txtKodeKonsumen.getText()+"'";
            java.sql.Connection conn = (Connection)koneksi.getkoneksi();
            java.sql.PreparedStatement pstm = conn.prepareStatement(sql);
            pstm.execute();
            JOptionPane.showMessageDialog(null,"Edit data berhasil..");
            tampilkan_data();
            kosongkan_foam();
        }catch(HeadlessException | SQLException e){
            JOptionPane.showMessageDialog(this,e.getMessage());
        }
        
    }//GEN-LAST:event_btEditActionPerformed

    private void tabelBiayaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelBiayaMouseClicked
        // TODO add your handling code here:
        int baris = tabelBiaya.rowAtPoint(evt.getPoint());
        String jab = tabelBiaya.getValueAt(baris, 1).toString();
        cbKodeMontir.setSelectedItem(jab);
        
        String ksmn = tabelBiaya.getValueAt(baris, 2).toString();
        txtKodeKonsumen.setText(ksmn);
        
        String mobil = tabelBiaya.getValueAt(baris, 3).toString();
        txtKodeMobil.setText(mobil);
        
        String kdtr = tabelBiaya.getValueAt(baris, 4).toString();
        txtKodeTransaksi.setText(kdtr);
        
        String tgltr = tabelBiaya.getValueAt(baris, 5).toString();
        txtTanggalTransaksi.setText(tgltr);
        
        String ttr = tabelBiaya.getValueAt(baris, 6).toString();
        txtTotalTransaksi.setText(ttr);
    }//GEN-LAST:event_tabelBiayaMouseClicked

    private void btHapusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btHapusActionPerformed
        // TODO add your handling code here:
        try{
            String sql = "DELETE FROM tb_biaya WHERE kode_konsumen = '"+txtKodeKonsumen.getText()+"'";
            java.sql.Connection conn = (Connection)koneksi.getkoneksi();
            java.sql.PreparedStatement pstm = conn.prepareStatement(sql);
            pstm.execute();
            JOptionPane.showMessageDialog(null,"HAPUS data berhasil..");
            tampilkan_data();
            kosongkan_foam();
        }catch(HeadlessException | SQLException e){
            JOptionPane.showMessageDialog(this,e.getMessage());
        }
    }//GEN-LAST:event_btHapusActionPerformed

    private void txtKodeKonsumenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtKodeKonsumenActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtKodeKonsumenActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(tb_biaya.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(tb_biaya.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(tb_biaya.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(tb_biaya.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new tb_biaya().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btEdit;
    private javax.swing.JButton btHapus;
    private javax.swing.JButton btKeluar;
    private javax.swing.JButton btSimpan;
    private javax.swing.JButton btTambah;
    private javax.swing.JComboBox<String> cbKodeMontir;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tabelBiaya;
    private javax.swing.JTextField txtKodeKonsumen;
    private javax.swing.JTextField txtKodeMobil;
    private javax.swing.JTextField txtKodeTransaksi;
    private javax.swing.JTextField txtTanggalTransaksi;
    private javax.swing.JTextField txtTotalTransaksi;
    // End of variables declaration//GEN-END:variables
}
